package com.macaku.component;

import cn.hutool.core.bean.BeanUtil;
import com.macaku.domain.dto.ModelMessage;
import com.macaku.domain.po.Email;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2024-01-14
 * Time: 22:47
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class AtomicEmailSender {
    private final JavaMailSender javaMailSender;

    private final TemplateEngine templateEngine;



    public void sendSimpleMailMessage(Email email) {
        if(Objects.isNull(email)) {
            log.warn("email不能为null！");
            return;
        }
        // 封装simpleMailMessage对象
        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setFrom(email.getSender());
        simpleMailMessage.setTo(email.getRecipient());
        simpleMailMessage.setCc(email.getCc());
        simpleMailMessage.setSubject(email.getTitle());
        simpleMailMessage.setText(email.getContent());
        // 发送
        javaMailSender.send(simpleMailMessage);
    }


    public void sendMailWithFile(Email email, File file) {
        if(Objects.isNull(email)) {
            log.warn("email不能为null！");
            return;
        }
        if(Objects.isNull(file)) {
            log.warn("file不能为null！");
            return;
        }
        // 封装对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(email.getSender());
            mimeMessageHelper.setTo(email.getRecipient());
            mimeMessageHelper.setCc(email.getCc());
            mimeMessageHelper.setSubject(email.getTitle());
            mimeMessageHelper.setText(email.getContent());
            // 添加附件
            mimeMessageHelper.addAttachment(file.getName(), file);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendModelMail(Email email, String template, Object modelMessage) {
        if(Objects.isNull(email)) {
            log.warn("email不能为null！");
            return;
        }
        // 封装对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(email.getSender());
            mimeMessageHelper.setTo(email.getRecipient());
            mimeMessageHelper.setCc(email.getCc());
            mimeMessageHelper.setSubject(email.getTitle());
            // 构造模板消息
            Context context = new Context();
            context.setVariables(BeanUtil.beanToMap(modelMessage));
            //合并模板与数据
            String content = templateEngine.process(template, context);
            mimeMessageHelper.setText(content);
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}
